package handler

import (
	"context"
	"gozero-microservices/gateway-api/internal/logic"
	"gozero-microservices/gateway-api/internal/types"
	"gozero-microservices/gateway-api/utils"
	"net/http"
	"strconv"

	"github.com/zeromicro/go-zero/rest/httpx"
)

/*
 +----------------------------------------------------------------------
 + Title        : NavHandler
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2025-08-08
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 导航处理器
 +----------------------------------------------------------------------
*/

type NavHandler struct {
	BaseHandler
}

/**
 * 新增
 *
 * @param string Title ------------------------必传,标题
 * @param string Link -------------------------必传,链接
 * @param uint32 Position ---------------------必传,位置
 * @param uint32 IsOpenNew --------------------非必传,是否打开新页面
 * @param string RelationGoods ----------------非必传,排序关联商品
 * @param uint32 Sort -------------------------非必传,排序
 * @return json
 * @author huwl
 * @date 2025/10/23 10:12:43
 */
func (control *NavHandler) Add(w http.ResponseWriter, r *http.Request) {
	var req types.NavAddRequest
	if err := httpx.Parse(r, &req); err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	// 调用逻辑层
	navLogic := logic.NewNavLogicInstance(context.Background(), control.SvcCtx)
	err := navLogic.Add(req)
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithMessage("新增导航成功"))
}

/**
 * 删除
 *
 * @param string Ids --------------------------必传,Id支持多个
 * @return json
 * @author huwl
 * @date 2025/10/20 18:51:38
 */
func (control *NavHandler) Delete(w http.ResponseWriter, r *http.Request) {
	ids := utils.DefaultQuery(r, "ids", "")

	// 调用逻辑层
	navLogic := logic.NewNavLogicInstance(context.Background(), control.SvcCtx)
	err := navLogic.Delete(ids)
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithMessage("删除导航成功"))
}

/**
 * 编辑
 *
 * @param string Id ---------------------------必传,Id
 * @param string Title ------------------------非必传,标题
 * @param string Link--------------------------非必传,链接
 * @param uint32 Position ---------------------非必传,位置
 * @param uint32 IsOpenNew --------------------非必传,是否打开新页面
 * @param string RelationGoods ----------------非必传,关联商品
 * @param uint32 Sort -------------------------非必传,排序
 * @return json
 * @author huwl
 * @date 2025/10/23 10:12:43
 */
func (control *NavHandler) Edit(w http.ResponseWriter, r *http.Request) {
	var req types.NavEditRequest
	if err := httpx.Parse(r, &req); err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	// 调用逻辑层
	navLogic := logic.NewNavLogicInstance(context.Background(), control.SvcCtx)
	err := navLogic.Edit(req)
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithMessage("编辑导航成功"))
}

/**
 * 编辑状态
 *
 * @param string ids --------------------------必传,Id支持多个
 * @param int status --------------------------必传,状态
 * @return json
 * @author huwl
 * @date 2025/10/23 10:15:55
 */
func (control *NavHandler) EditStatus(w http.ResponseWriter, r *http.Request) {
	var req types.BannerEditStatusRequest
	if err := httpx.Parse(r, &req); err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	// 调用逻辑层
	navLogic := logic.NewNavLogicInstance(context.Background(), control.SvcCtx)
	result, err := navLogic.EditStatus(req)
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithMessage(result.Msg))
}

/**
 * 详情
 *
 * @param int id ------------------------------必传,Id
 * @return json
 * @author huwl
 * @date 2025/10/23 10:15:55
 */
func (control *NavHandler) Detail(w http.ResponseWriter, r *http.Request) {
	id, _ := strconv.Atoi(utils.DefaultQuery(r, "id", "0"))

	// 调用逻辑层
	navLogic := logic.NewNavLogicInstance(context.Background(), control.SvcCtx)
	result, err := navLogic.Detail(uint16(id))
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithData(result))
}

/**
 * 列表
 *
 * @param uint16 Id ---------------------------非必传,Id
 * @param string Ids --------------------------非必传,Id支持多个
 * @param string Title ------------------------非必传,标题
 * @param uint8 Position ----------------------非必传,位置
 * @param string IsOpenNew --------------------非必传,是否打开新页面
 * @param string Status -----------------------非必传,状态
 * @param string StartCreateTime --------------非必传,开始创建时间
 * @param string EndCreateTime ----------------非必传,结束创建时间
 * @param string SortField --------------------非必传,排序字段
 * @param string SortType ---------------------非必传,排序方式
 * @param int8 PageSize -----------------------非必传,条数
 * @param uint8 Page --------------------------非必传,页码
 * @return json
 * @author huwl
 * @date 2025/10/23 10:12:43
 */
func (control *NavHandler) List(w http.ResponseWriter, r *http.Request) {
	var req types.NavQueryRequest
	if err := httpx.Parse(r, &req); err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	// 调用逻辑层
	navLogic := logic.NewNavLogicInstance(context.Background(), control.SvcCtx)
	result, err := navLogic.List(req)
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithData(map[string]interface{}{
		"page":       result.Page,
		"page_size":  result.PageSize,
		"total":      result.Total,
		"page_total": result.PageTotal,
		"navs":       result.Navs,
	}))
}

/**
 * 列表
 *
 * @param uint16 Id ---------------------------非必传,Id
 * @param string Ids --------------------------非必传,Id支持多个
 * @param string Title ------------------------非必传,标题
 * @param uint8 Position ----------------------非必传,位置
 * @param string IsOpenNew --------------------非必传,是否打开新页面
 * @param string Status -----------------------非必传,状态
 * @param string StartCreateTime --------------非必传,开始创建时间
 * @param string EndCreateTime ----------------非必传,结束创建时间
 * @return json
 * @author huwl
 * @date 2025/10/23 10:12:43
 */
func (control *NavHandler) Total(w http.ResponseWriter, r *http.Request) {
	var req types.NavQueryRequest
	if err := httpx.Parse(r, &req); err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	// 调用逻辑层
	navLogic := logic.NewNavLogicInstance(context.Background(), control.SvcCtx)
	result, err := navLogic.Total(req)
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithData(map[string]interface{}{
		"total": result.Total,
	}))
}
